﻿using System;
using _1___Graph;

namespace _2___Factory_Pattern {
    class InvalidGraphTypeException : Exception {
        public InvalidGraphTypeException(string message) : base(message) { }
    }

    class Factory {
        static private Random random = new Random();

        public static Graph GetGraph(int type, int[] info) {
            Graph graph;
            switch (type) {
                case 0:
                    graph = new Triangle(info[0], info[1], info[2]);
                    break;
                case 1:
                    graph = new Rectangle(info[0], info[1]);
                    break;
                case 2:
                    graph = new Square(info[0]);
                    break;
                default:
                    throw new InvalidGraphTypeException("Graph type is invalid.");
            }
            if (!graph.IsValid()) {
                throw new InvalidGraphException("Graph is invalid.");
            }
            return graph;
        }
        public static Graph GetRandomGraph(out int type) {
            type = random.Next(0, 3);
            int[] info;
            do {
                switch (type) {
                    case 0:
                        info = new int[] { random.Next(1, 5), random.Next(1, 6), random.Next(1, 7) };
                        break;
                    case 1:
                        info = new int[] { random.Next(1, 5), random.Next(1, 6) };
                        break;
                    case 2:
                        info = new int[] { random.Next(1, 5) };
                        break;
                    default:
                        info = new int[] { 1 };
                        break;
                }
                try {
                    return GetGraph(type, info);
                }
                catch (InvalidGraphException e) { }
            } while (true);
        }
    }
    class Program {
        static void Main(string[] args) {
            int type;
            double areaSum = 0;
            Graph[] graph = new Graph[10];
            for (int i = 0; i < 10; i++) {
                graph[i] = Factory.GetRandomGraph(out type);
                switch (type) {
                    case 0:
                        Console.WriteLine("Triangle({0},{1},{2})", graph[i][0], graph[i][1], graph[i][2]);
                        break;
                    case 1:
                        Console.WriteLine("Rectangle({0},{1})", graph[i][0], graph[i][1]);
                        break;
                    case 2:
                        Console.WriteLine("Square({0})", graph[i][0]);
                        break;
                }
                areaSum += graph[i].Area;
            }
            Console.WriteLine("Area Sum: {0}", areaSum.ToString("N2"));
        }
    }
}
